package com.example.exam.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.exam.domain.AcademyDO;
import com.example.exam.domain.PaperDO;
import com.example.exam.domain.PaperSubmitLogDO;
import com.example.exam.domain.StudentDO;
import com.example.exam.domain.vo.PaperSubmitLogVO;
import com.example.exam.service.IAcademyService;
import com.example.exam.service.IPaperService;
import com.example.exam.service.IPaperSubmitLogService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@AllArgsConstructor
public class PaperSubmitLogController {

    private final IPaperSubmitLogService paperSubmitLogService;

    private final IAcademyService academyService;

    private final IPaperService paperService;

    @GetMapping("/my_exam.html")
    public String myExamPage(HttpSession session,Model model){
        StudentDO loginUser = (StudentDO)session.getAttribute("loginUser");
        if (BeanUtil.isEmpty(loginUser)) {
            return "my_exam";
        }
        AcademyDO byIdAcademyDO = academyService.getById(loginUser.getAId());
        List<PaperDO> listPaperDO = paperService.list(new LambdaQueryWrapper<PaperDO>()
                .eq(PaperDO::getAId, byIdAcademyDO.getId()));
        List<Integer> collect = listPaperDO.stream().map(item -> item.getId()).collect(Collectors.toList());


        List<PaperSubmitLogDO> listPaperSubmitLogDO = paperSubmitLogService.listByIds(collect);
        List<PaperSubmitLogVO> collectPaperSubmitLogVO = listPaperSubmitLogDO.stream().map(item -> {
            PaperSubmitLogVO paperSubmitLogVO = BeanUtil.copyProperties(item, PaperSubmitLogVO.class);
            PaperDO byIdPaperDO = paperService.getById(item.getPId());
            paperSubmitLogVO.setPaper(byIdPaperDO);
            return paperSubmitLogVO;
        }).collect(Collectors.toList());

        model.addAttribute("listPaperSubmitLog", collectPaperSubmitLogVO);
        return "my_exam";
    }
}
